package com.shz.appletsapi.common.utils;



import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class PwdUtil {

	public static final String KEY_ALGORITHM = "AES";
	public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
	public static final String VIPARA = "Z0DXkfxpjOadvMtI";
    public static final String PSWD = "apple";

	private static byte[] encrypt(String content, String password) {
		try {
			SecretKeySpec skeySpec = getKey(password);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			IvParameterSpec iv = new IvParameterSpec(VIPARA.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			return cipher.doFinal(content.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	private static byte[] decrypt(byte[] content, String password) {
		try {
			SecretKeySpec skeySpec = getKey(password);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			IvParameterSpec iv = new IvParameterSpec(VIPARA.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] original = cipher.doFinal(content);
			return original;
		} catch (NoSuchAlgorithmException e) {
			return null;
		} catch (IllegalBlockSizeException e) {
			return null;
		} catch (BadPaddingException e) {
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	private static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	private static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static String encryptString(String srcStr, String password)
			throws UnsupportedEncodingException {
		String resultStr = "";
		byte[] result = encrypt(srcStr, password);
		resultStr = bytesToHexString(result);
		return resultStr;

	}

	public static String decryptString(String destStr, String password) {
		String resultStr = "";
		try {
			byte[] result = hexStringToBytes(destStr);
			result = decrypt(result, password);

			resultStr = new String(result, "UTF-8");
		} catch (Exception e) {
			return null;
		}
		return resultStr;
	}

	private static SecretKeySpec getKey(String strKey) {
		byte[] arrBTmp = strKey.getBytes();
		byte[] arrB = new byte[16]; 

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}

		SecretKeySpec skeySpec = new SecretKeySpec(arrB, KEY_ALGORITHM);

		return skeySpec;
	}


    public static void main(String[] args) {
        String aaa = "5145050921f55f23f2c70a0a47920f2a8a17bae365fd8e829b70b16209461f79a6d9a1268578345c3feaa9e7f01ad82f0ee6c1dff151f73cad3281020e28131c479394b26d93492d0b712e42fc304350465c2ac80a934d36cbd1d0046f1dee318e8610c79983c165cbd162be021e6835448101477b555e7eed4f662e06206fb7fc4ccb2a8aae8d43d17abbfd5f546e010058d49cf0444311165aff6e5838219c06296bf93524b1f50ea5baaba0138b4877239152ea1dbeca6a4f04abf3a7632d613726129963af55630e4028fdce2d5248f3a671a4aee02b62113973dbe79b67386dbd6a2a960dca2cdfd57f7e181a43e34d030749ce37a6853774d868d2389c98495ff00002c7c9f33fda70bd848e98d95e8b35c00e63af48a03a6a2900dfc8a4e363abf47a9f8e5a6c8a048d05e92305da58071ee4f5c9d16373a292c7d179b5ca1b6ae74b95adafe83e90559e95cc9ff0deb96a33496d737736a55ba2a84a5fb5258492f8eae1cbedcdfc193ed87a4bbc420feee19d3ab94125bfadb8e499d2035fc9c27937c275ec173e60957ac8966f02342cc41cd279bbf3a9261258c92d345fde7968af079a9b59f4f63172ea";
        String bbb = null;
        try {
            bbb = encryptString(aaa, PSWD);
            String ccc = encryptString("userId=59c216c4-f95c-4b44-9460-b6502e3f8f53d&version=1&channelId=chrome_01&pkgId=13", PSWD);
            String ccc1 = encryptString("channel=hl_01&userId=test", PSWD);
            System.out.println(ccc1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
